package com.jye.cloud.user.service.impl;


import com.jye.cloud.Constants.MyConstants;
import com.jye.cloud.domain.Payment;
import com.jye.cloud.user.service.PaymentService;
import com.jye.cloud.user.mapper.PaymentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    PaymentMapper paymentMapper;

    @Override
    public Payment findPayById(int id) {
        Payment payment = paymentMapper.findPayById(id);
        return payment;
    }

    @Override
    public int addPay(Payment payment) {
        try {
            if (payment.getStatus()==null){
                payment.setStatus(MyConstants.payStatus.unpaid);
            }
            payment.setCreateTime(new Date());
            paymentMapper.addPay(payment);
            return 1;
        }catch (Exception e){
            System.out.println(e);
            return 0;
        }

    }

    @Override
    public List<Payment> findMyPayByUId(int uId, int skip, int limit) {
        List<Payment> paymentList = paymentMapper.findMyPayByUId(uId,skip,limit);
        return paymentList;
    }

    @Override
    public List<Payment> findMyPayByUIdAndStatus(int uId, String status, int skip, int limit) {
        List<Payment> paymentList = paymentMapper.findMyPayByUIdAndStatus(uId,status,skip,limit);
        return paymentList;
    }

    @Override
    public String confrimPayById(int id) {
        try{
            Payment payment = new Payment();
            //id
            payment.setId(id);
            //状态
            String status = MyConstants.payStatus.confirmed;
            payment.setStatus(status);
            //时间
            payment.setConfirmTime(new Date());
            paymentMapper.updatePay(payment);
            return "确认订单成功!";
        }catch (Exception e){
            return "确认失败!请重试!";
        }
    }

    @Override
    public String cancelPayById(int id) {
        try{
            Payment payment = new Payment();
            //id
            payment.setId(id);
            //状态
            String status = MyConstants.payStatus.cancel;
            payment.setStatus(status);

            paymentMapper.updatePay(payment);
            return "取消订单成功!";
        }catch (Exception e){
            return "取消失败!请重试!";
        }
    }

    @Override
    public int paidPayById(int id) {
        try{
            Payment payment = new Payment();
            //id
            payment.setId(id);
            //状态
            String status = MyConstants.payStatus.paid;
            payment.setStatus(status);
            //时间
            payment.setPayTime(new Date());

            paymentMapper.updatePay(payment);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public String frozenPayById(int id) {
        try{
            Payment payment = new Payment();
            //id
            payment.setId(id);
            //状态
            String status = MyConstants.payStatus.unavailable;
            payment.setStatus(status);

            paymentMapper.updatePay(payment);
            return "冻结订单成功!";
        }catch (Exception e){
            return "冻结失败!请重试!";
        }
    }

    @Override
    public Payment findPayByUIdAndCId(int uId, int cId) {
        Payment payment = paymentMapper.findPayByUIdAndCId(uId,cId);
        return payment;
    }

    @Override
    public int findPayStatusByPId(int pId) {
        String payStatusByPId = paymentMapper.findPayStatusByPId(pId);
        String isPaid = MyConstants.payStatus.paid;
        if(isPaid.equals(payStatusByPId)){
            //为已支付
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public List<Payment> findMyPayByUIdAndType(int uId, String type, int skip, int limit) {
        return paymentMapper.findMyPayByUIdAndType(uId,type,skip,limit);
    }
}
